package br.com.iswe.gwt.ui.client;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import br.com.iswe.core.entidades.gwt.BeanMap;

import com.google.gwt.junit.client.GWTTestCase;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class FormFactoryTest extends GWTTestCase {
	private static final String CHECK_BOX = "checkBox";
	private static final String LIST_BOX = "listBox";
	private static final String TEXT_AREA = "textArea";
	private static final String PASSWORD_TEXT_BOX = "passwordTextBox";
	private static final String DATE_BOX = "dateBox";
	private static final String DATE_TIME_BOX = "dateTimeBox";
	private static final String RADIO_GROUP_TESTE = "radioGroupTeste";
	private static final String TEXT_BOX = "textBox";

	public String getModuleName() {
		return "br.com.iswe.gwt.ui.ISWEGwtLibrary";
	}
	
	TextBox textBox;
	
	RadioGroup radioGroup;
	
	RadioButton radioButton1;
	RadioButton radioButton2;
	RadioButton radioButton3;
	
	DateTimeBox dateTimeBox;
	
	DateBox dateBox;

	PasswordTextBox passwordTextBox;
	
	TextArea textArea;
	
	ListBox listBox;
	
	CheckBox checkBox;
	
	public void testPopulateFormProperties_Entity(){
		//Create thr instance of the formFactory
		DomainFormFactory<EntityTest> factory = new DomainFormFactory<EntityTest>();
		
		//add the properties to the factory
		addProperties(factory);
		
		//Create the entity containing the value to populate the form factory.
		EntityTest entityTest = new EntityTest();
		
		entityTest.setTextBox("textBox-value");
		
		entityTest.setRadioGroupTeste(2);
		
		Timestamp now = new Timestamp(System.currentTimeMillis());
		entityTest.setDateTimeBox(now);
		//beanMap.put(DATE_TIME_BOX, 		"15/02/2001 12:45");
		
		Date today = new Date();
		entityTest.setDateBox(today);
		//beanMap.put(DATE_BOX, 			"18/03/2010");
		
		entityTest.setPasswordTextBox("123");
		
		entityTest.setTextArea("big text ...");
		
		entityTest.setListBox(2);
		
		entityTest.setCheckBox(true);
		
		DateInfo dateFmt = new DateInfo(entityTest.getDateBox());		
		
		DateInfo timeFmt = new DateInfo(entityTest.getDateTimeBox());		
		//SimpleDateFormat timeFmt = new SimpleDateFormat("dd/MM/yyyy hh:mm");
		
		//populate the form
		factory.populateFormProperties(entityTest);
		
		//asserts
		assertEquals(entityTest.getTextBox(), textBox.getText());
		assertEquals(entityTest.getCheckBox(), checkBox.getValue());
		assertEquals(entityTest.getListBox().toString(), listBox.getValue(listBox.getSelectedIndex()));
		assertEquals(entityTest.getTextArea(), textArea.getText());
		assertEquals(entityTest.getPasswordTextBox(), passwordTextBox.getText());
		
		String dateString = dateFmt.format( );
		assertEquals(dateString, dateBox.getValue());
		
		String timeString = timeFmt.formatDateTime();
		assertEquals(timeString, dateTimeBox.getValue());
		
		assertEquals(entityTest.getRadioGroupTeste().toString(), radioGroup.getValue());
	}
	
	@SuppressWarnings("unchecked")
	public void testPopulateFormProperties_BeanMap_ListProperties(){
		
		//Create thr instance of the formFactory
		BeanMapFormFactory factory = new BeanMapFormFactory();
		
		//multiple checkboxes
		CheckBox ck1 = new CheckBox("ck1");
		factory.addProperty("checkbox", ck1);
		CheckBox ck2 = new CheckBox("ck2");
		factory.addProperty("checkbox", ck2);
		CheckBox ck3 = new CheckBox("ck3");
		factory.addProperty("checkbox", ck3);
		CheckBox ck4 = new CheckBox("ck4");
		factory.addProperty("checkbox", ck4);
		CheckBox ck5 = new CheckBox("ck5");
		factory.addProperty("checkbox", ck5);
		
		factory.getPropertyInfo("checkbox").setListPropertyName("optionList");
		
		ck3.setValue(true);
		
		BeanMap values = factory.getMapValues();
		
		Object checkboxProperty = values.getPropertyValue("optionList");
		
		assertNotNull(checkboxProperty);
		assertTrue(checkboxProperty instanceof List );
		assertTrue(((List)checkboxProperty).size() == 5);
		assertTrue(((List)checkboxProperty).get(0) instanceof String);
		assertEquals(((List)checkboxProperty).get(2), "true");
	}
	
	public void testPopulateFormProperties_BeanMap(){
		
		//Create thr instance of the formFactory
		BeanMapFormFactory factory = new BeanMapFormFactory();
		
		//add the properties
		addProperties(factory);
		
		//Create the BeanMap containing the value to populate the form factory.
		BeanMap beanMap = new BeanMap();
		
		beanMap.put(TEXT_BOX, 			"textBox-value");
		beanMap.put(RADIO_GROUP_TESTE, 	"2");
		beanMap.put(DATE_TIME_BOX, 		"15/02/2001 12:45");
		beanMap.put(DATE_BOX, 			"18/03/2010");
		beanMap.put(PASSWORD_TEXT_BOX, 	"123");
		beanMap.put(TEXT_AREA, 			"big text ...");
		beanMap.put(LIST_BOX, 			"2");
		beanMap.put(CHECK_BOX, 			"true");
		
		//populate the formfactory
		factory.populateFormProperties(beanMap);
		
		//check the components
		assertEquals(beanMap.getString(TEXT_BOX), textBox.getText());
		
		assertTrue(radioButton2.getValue());
		
		assertEquals("15/02/2001", dateTimeBox.getDateBox().getValue());
		assertEquals("12:45", dateTimeBox.getTimeBox().getValue());
		assertEquals(beanMap.getString(DATE_TIME_BOX), dateTimeBox.getValue());
		
		assertEquals(beanMap.getString(DATE_BOX), dateBox.getValue());
		
		assertEquals(beanMap.getString(PASSWORD_TEXT_BOX), passwordTextBox.getText());
		
		assertEquals(beanMap.getString(TEXT_AREA), textArea.getText());
		
		assertEquals(listBox.getSelectedIndex(), 1);
		
		assertTrue(checkBox.getValue());
		
	}

	private void addProperties(FormFactoryAbstract factory) {
		//String property
		factory.buildProperty("fixed-string", "value-fixed-string");
		
		//text box
		textBox = new TextBox();
		factory.addProperty(TEXT_BOX, textBox);
		
		//Radio Buttons
		radioButton1 = new RadioButton("teste");
		radioButton2 = new RadioButton("teste");
		radioButton3 = new RadioButton("teste");
		//radioGroup
		radioGroup = new RadioGroup();
		radioGroup.addRadioButton(radioButton1, "1");
		radioGroup.addRadioButton(radioButton2, "2");
		radioGroup.addRadioButton(radioButton3, "3");
		factory.addProperty(RADIO_GROUP_TESTE, radioGroup);
		
		//DateTimeBox
		dateTimeBox = new DateTimeBox();
		factory.addProperty(DATE_TIME_BOX, "DateTimeBox Label", dateTimeBox);
		
		//DateBox
		dateBox = new DateBox();
		factory.addProperty(DATE_BOX, "dateBox label", dateBox);
		
		//PasswordTextBox
		passwordTextBox = new PasswordTextBox();
		factory.addProperty(PASSWORD_TEXT_BOX, passwordTextBox);
		
		//TextArea
		textArea = new TextArea();
		factory.addProperty(TEXT_AREA, textArea);
		
		//ListBox
		listBox = new ListBox();
		factory.addProperty(LIST_BOX, listBox);
		
		//CheckBox
		checkBox = new CheckBox();
		factory.addProperty(CHECK_BOX, checkBox);
		
		
		//populate the list box component
		listBox.addItem("Option 1", "1");
		listBox.addItem("Option 2", "2");
		listBox.addItem("Option 3", "3");
	}	
}
